package com.backweb.Controller;

import com.backweb.dao.UserDao;
import com.backweb.model.LoginParam;
import com.backweb.model.User;
import com.backweb.model.UserParam;
import com.backweb.tools.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class LoginContrtoller {
    @Autowired
    private UserDao userDao;


    @GetMapping("/userAll")
    public ResultMap userAll() {

       List<User>  items=new ArrayList<>();
        try {
            items= userDao.findAll();
        }catch (Exception ex){}
        Map map = new HashMap();
        map.put("items",items);
        return  ResultMap.success(map);

    }
    @GetMapping("/getuser")
    public ResultMap getuserid(int userid) {
        User user=new User();
        try {
            user=userDao.findUsersByUserid(userid);
        }catch (Exception ex){}
        Map map = new HashMap();
        map.put("user",user);
        return  ResultMap.success(map);

    }



    @PostMapping("/userDel")
    public ResultMap userDel(@RequestBody UserParam param) {
        try {
            User  user=userDao.findUsersByUserid(param.getRid());
            if(user!=null&&user.getUserid()>0) {
                userDao.delete(user);
            }
        }catch (Exception ex){}


        return  ResultMap.success();

    }

    @PostMapping("/userSave")
    public ResultMap userSave(@RequestBody UserParam param) {
        Map map = new HashMap();
        User user=null;
        try {
             user=userDao.findUsersByUserid(param.getRid());
            if(user!=null&&user.getUserid()>0) {
                user.setUserPwd(param.getPassword());
                User user1=userDao.save(user);
                map.put("user",user1);
            }else
            {
                user=new User();
                user.setUserPwd(param.getPassword());
                user.setUserName(param.getUsername());
                user.setUserReal(param.getUserReal());
                user.setUserTel(param.getUserTel());
                User user1=userDao.save(user);
                map.put("user",user1);

            }
        }catch (Exception ex){}


        return  ResultMap.success(map);

    }



    @GetMapping("/userinfo")
    public ResultMap userinfo(HttpSession session) {

         User user=new User();
         try {
              user= userDao.findUsersByUserName("admin");

         }catch (Exception ex){}
        Map map = new HashMap();
        map.put("user",user);
        return  ResultMap.success(map);

    }
    @PostMapping("/logout")
    public ResultMap logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return  ResultMap.success();
    }


    @PostMapping("/login")
    public ResultMap userLogin(@RequestBody LoginParam param, HttpServletRequest request) throws Exception {
        User user= userDao.findUsersByUserName(param.getUsername());
        if(user!=null&&user.getUserid()>0)
        {
           if(user.getUserPwd().equals(param.getPassword()))
           {
               HttpSession session = request.getSession();
               session.setAttribute("user",user);
               User   user1 = (User) session.getAttribute("user");
               Map map = new HashMap();
               map.put("user",user1);
               return  ResultMap.success(map,"admin-token");

           }
           else
           {
               return  ResultMap.failed();
           }

        }else
        {
            return  ResultMap.failed();

        }

    }


    @GetMapping("/login1")
    public ResultMap login1(String name, String pwd, HttpServletRequest request) throws Exception {
        User user= userDao.findUsersByUserName(name);
        if(user!=null&&user.getUserid()>0)
        {
            if(user.getUserPwd().equals(pwd))
            {
                HttpSession session = request.getSession();
                session.setAttribute("user",user);
                Map map = new HashMap();
                map.put("user",user);
                return  ResultMap.success(map,"admin-token");

            }
            else
            {
                return  ResultMap.failed();
            }

        }else
        {
            return  ResultMap.failed();

        }

    }

}
